<template>
  <div class="home_container" id="home_container">
    <div :class="{home: true, [remBy]: true}" id="home">
      <div class="nav-wrap">
        <div class="menu-wrap" @mouseenter="handleFMenu" @mouseleave="handleFMenu">
          <div class="go-admin">进入后台</div>
          <div style="display: flex;align-items: flex-start;">
            <div class="menu" v-show="fMenuShow" ref="menu">
              <template v-for="(route, index) in sidebarRouters">
                <div
                  v-if="!route.hidden && route.meta"
                  class="menu-item"
                  :style="curMenuIdx == index ? 'color: #73FCFD;' : ''"
                  :key="route.path  + index"
                  @mouseenter="handleRoute(route, index)"
                >
                  <span>{{ (route.meta && route.meta.title) || (route.children[0].meta && route.children[0].meta.title) }}</span>
                  <i class="el-icon-arrow-right"></i>
                </div>
              </template>
            </div>
            <div
              class="sub-menu"
              :style="`margin-top: ${subTop};margin-left: ${subLeft}`"
              v-show="sMenuShow"
            >
              <template v-for="(route, index) in subMenu">
                <div
                  v-if="!route.hidden"
                  class="sub-menu-item"
                  :style="curSubItemIdx == index ? 'color:#73FCFD':''"
                  @mouseenter="curSubItemIdx = index"
                  @mouseleave="curSubItemIdx = null"
                  :key="route.path  + index"
                  @click="handleSubRoute(route)"
                >
                  <span>{{ route.meta && route.meta.title }}</span>
                  <!-- <i class="el-icon-arrow-right"></i> -->
                </div>
              </template>
            </div>
          </div>
        </div>
        <div class="nav-title">{{school}}安全用电管理平台</div>
        <div class="user">
          <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
            <div class="avatar-wrapper">
              <el-avatar :src="avatar"></el-avatar>
              <div class="name">{{name}}</div>
              <i class="el-icon-caret-bottom" style="color: #fff"></i>
            </div>
            <el-dropdown-menu slot="dropdown">
              <router-link to="/user/profile">
                <el-dropdown-item>个人中心</el-dropdown-item>
              </router-link>
              <!-- <el-dropdown-item @click.native="setting = true">
              <span>布局设置</span>
              </el-dropdown-item>-->
              <el-dropdown-item divided @click.native="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <div class="content-wrap">
        <div class="top">
          <div class="left">
            <div class="meter-status pd20">
              <div class="panel-title">
                <div class="title">电表状态</div>
              </div>
              <MeterStatus />
            </div>
            <div class="malignant-load pd20">
              <div class="panel-title">
                <div class="title">违规监控</div>
              </div>
              <MalignantLoad />
            </div>
          </div>
          <div class="center">
            <Center />
          </div>
          <div class="right">
            <div class="room pd20">
              <div class="panel-title">
                <div class="title">宿舍情况</div>
              </div>
              <Room />
            </div>
          </div>
        </div>
        <div class="bottom">
          <div class="bottom-item pd20">
            <div class="panel-title">
              <div class="title">违规统计</div>
              <el-date-picker v-model="value1" type="date" placeholder="选择日期"></el-date-picker>
            </div>
            <Violations :date="value1" v-if="remBy" />
          </div>
          <div class="bottom-item pd20">
            <div class="panel-title">
              <div class="title">用电分析</div>
              <el-date-picker v-model="value2" type="date" placeholder="选择日期"></el-date-picker>
            </div>
            <Electricity :date="value2" v-if="remBy" />
          </div>
          <div class="bottom-item pd20">
            <div class="panel-title">
              <div class="title">宿舍人流</div>
              <el-date-picker v-model="value3" type="date" placeholder="选择日期"></el-date-picker>
            </div>
            <PedestrianFlow :date="value3" v-if="remBy" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import MeterStatus from "./components/MeterStatus";
import MalignantLoad from "./components/MalignantLoad";
import Center from "./components/Center";
import Room from "./components/Room";
import Violations from "./components/Violations";
import PedestrianFlow from "./components/PedestrianFlow";
import Electricity from "./components/Electricity";
import { mapGetters } from "vuex";
export default {
  name: "Home",
  components: {
    MeterStatus,
    MalignantLoad,
    Center,
    Room,
    Violations,
    PedestrianFlow,
    Electricity
  },
  data() {
    return {
      curSubItemIdx: null,
      value1: Date.now(),
      value2: Date.now(),
      value3: Date.now(),
      fMenuShow: false,
      sMenuShow: false,
      subMenu: [],
      subTop: "0rem",
      // subLeft: '10.6875rem',
      subLeft: "0.625rem",
      curMenuIdx: null,
      remBy: null
    };
  },
  computed: {
    ...mapGetters(["sidebarRouters", "avatar", "school", "name"]),
    rangeRoute() {
      return this.sidebarRouters.filter(route => {
        // return route.hasOwnProperty('meta')
        return !route.hidden && route.hasOwnProperty("meta");
      });
    },
    setting: {
      get() {
        return this.$store.state.settings.showSettings;
      },
      set(val) {
        this.$store.dispatch("settings/changeSetting", {
          key: "showSettings",
          value: val
        });
      }
    }
  },
  mounted() {
    //实例被挂载后调用
    this.getContainerWidth();
    window.addEventListener("resize", this.getContainerWidth);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.getContainerWidth);
  },

  methods: {
    goAdmin() {
      this.$router.push("/index/index");
    },
    getContainerWidth() {
      let that = this;
      let elementResizeDetectorMaker = require("element-resize-detector");
      let erd = elementResizeDetectorMaker();
      erd.listenTo(document.getElementById("home_container"), element => {
        //监听div宽度变化
        let height = element.offsetHeight;
        let width = element.offsetWidth;
        this.remBy = "w";
        if (width / height > 120 / 67.5) {
          width = (height * 120) / 67.5;
          this.remBy = "h";
        }
        let fontSize = width / 120 + "px";
        let html = document.documentElement;
        html.style.fontSize = fontSize;
      });
    },
    handleFMenu() {
      this.fMenuShow = !this.fMenuShow;
      this.sMenuShow = false;
      this.curMenuIdx = null;
    },
    handleRoute(route, index) {
      if (route.meta) {
        this.curMenuIdx = index;
        this.sMenuShow = false;
        const idx = this.rangeRoute.findIndex(item => item == route);
        let initTop = 1.25;
        const step = 3.5;
        this.subTop = initTop + step * idx + "rem";
        this.subMenu = route.children;
        // if(this.$refs.menu) {
        //   this.subLeft = `${(this.$refs.menu.clientWidth + 40) / 16}rem`
        // }
        this.sMenuShow = true;
      } else {
        // console.log('path', route.path)
        this.$router.push({ name: route.children[0].name });
      }
    },
    handleSubRoute(route) {
      this.$router.push({ name: route.name });
    },
    async logout() {
      this.$confirm("确定注销并退出系统吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$store.dispatch("LogOut").then(() => {
            location.href = "/index";
          });
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>
.home_container {
  width: 100%;
  height: 100%;
  background-color: #000000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.home {
  display: flex;
  flex-direction: column;
  aspect-ratio: 120/67.5;
  background-color: #000;
  overflow: hidden;
  &.w {
    width: 100vmax;
  }
  &.h {
    height: 100vmin;
  }
}
.nav-wrap {
  width: 100%;
  height: 5.5rem;
  background-image: url("../../assets/images/home-nav.png");
  background-size: 100% 100%;
  position: relative;
  .menu-wrap {
    position: absolute;
    top: 0.75rem;
    left: 1.875rem;
    z-index: 3;
  }
  .go-admin {
    width: 7rem;
    height: 2rem;

    background-image: url("../../assets/images/backstage.png");
    background-size: 100% 100%;
    font-family: Microsoft YaHei, Microsoft YaHei;
    font-weight: 400;
    font-size: 0.875rem;
    color: #73fcfd;
    line-height: 2rem;
    padding-left: 1.125rem;
    cursor: pointer;
  }
  .menu {
    // min-width: 132px;
    // position: absolute;
    // left: 1.875rem;
    // top: 3.9375rem;
    margin-top: 20px;
    background-image: url("../../assets/images/menuBg.png");
    background-size: 100% 100%;
    padding: 0 20px;
    cursor: pointer;
    align-self: auto;
    .menu-item {
      min-width: 132px;
      padding: 20px 0;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 400;
      font-size: 14px;
      color: #4090b0;
      border-bottom: 1px solid #fff;
      border-image: linear-gradient(
          to right,
          rgba(5, 115, 148, 0),
          rgba(5, 115, 148, 0.2),
          rgba(5, 115, 148, 0.4),
          rgba(5, 115, 148, 0.6),
          rgba(5, 115, 148, 0.8),
          rgba(5, 115, 148, 1),
          rgba(5, 115, 148, 0.8),
          rgba(5, 115, 148, 0.6),
          rgba(5, 115, 148, 0.4),
          rgba(5, 115, 148, 0.2),
          rgba(5, 115, 148, 0)
        )
        1 10;
      span {
        margin-right: 10px;
      }
    }
    .menu-item:hover {
      color: #73fcfd;
    }
  }
  .sub-menu {
    // min-width: 132px;
    // position: absolute;
    background-image: url("../../assets/images/menuBg.png");
    background-size: 100% 100%;
    padding: 0 20px;
    // z-index: 3;
    cursor: pointer;
    .sub-menu-item {
      min-width: 132px;
      padding: 20px 0;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 400;
      font-size: 14px;
      color: #4090b0;
      span {
        margin-right: 10px;
      }
      .sub-menu-item:hover {
        color: #73fcfd;
      }
    }
  }
  .nav-title {
    text-align: center;
    line-height: 5.5rem;
    font-family: Microsoft YaHei, Microsoft YaHei;
    font-weight: bold;
    font-size: 1.875rem;
    color: #ffffff;
  }
  .user {
    position: absolute;
    top: 0.625rem;
    right: 1.25rem;
    cursor: pointer;
    .avatar-wrapper {
      display: flex;
      align-items: center;
      ::v-deep .el-avatar {
        width: 2.5rem;
        height: 2.5rem;
      }
    }
    .name {
      font-family: Microsoft YaHei, Microsoft YaHei;
      color: #fff;
      margin: 0 0.375rem;
      font-size: 0.875rem;
    }
  }
}
.content-wrap {
  width: 100%;
  overflow: hidden;
  flex: 1;
  padding: 1.875rem 1.875rem 1.25rem 1.875rem;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .pd20 {
    padding: 1.25rem;
    box-sizing: border-box;
  }
  .panel-title {
    width: 100%;
    height: 2.1875rem;
    background-image: url("../../assets/images/subtitle.png");
    background-size: 100% 100%;
    display: flex;
    justify-content: space-between;
    .title {
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: bold;
      font-size: 1rem;
      color: #f2f6fc;
      margin-left: 1.5rem;
      line-height: 2.125rem;
    }
    ::v-deep .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 9.375rem;
      .el-input__prefix {
        top: -0.125rem;
        .el-input__icon {
          width: 1rem;
        }
      }
      input {
        font-size: 0.875rem;
      }
    }
    ::v-deep .el-input--medium {
        font-size: 0.875rem;
      }
    ::v-deep .el-input__inner {
      background-color: #0f2438;
      width: 9.375rem;
      height: 2rem;
      line-height: 2rem;
      border: 0;
      color: #87929c;
      padding: 0 2rem;
    }
  }
  .top {
    width: 100%;
    display: flex;
    flex: 1;
    overflow: hidden;
    justify-content: space-between;
    .left {
      .meter-status {
        width: 28.125rem;
        height: 14.125rem;
        background: rgba(12, 29, 53, 0.5);
        border-radius: 0.25rem 0.25rem 0.25rem 0.25rem;
      }
      .malignant-load {
        width: 28.125rem;
        height: 24.75rem;
        background: rgba(12, 29, 53, 0.5);
        border-radius: 0.25rem 0.25rem 0.25rem 0.25rem;
        margin-top: 1.25rem;
      }
    }
    .center {
      width: calc(100% - 60rem);
      height: calc(100vh - 7.375rem - 20rem);
    }
    .right {
      .room {
        width: 28.125rem;
        height: 40.125rem;
        background: rgba(12, 29, 53, 0.5);
        border-radius: 0.25rem 0.25rem 0.25rem 0.25rem;
      }
    }
  }
  .bottom {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-top: 1.25rem;
    ::v-deep .el-input__icon {
      line-height: 100%;
    }
    .bottom-item {
      width: 37.875rem;
      height: 17.5rem;
      background: rgba(12, 29, 53, 0.5);
      border-radius: 0.625rem 0.625rem 0.625rem 0.625rem;
    }
  }
}
</style>